package bit.pja.wolfkill.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    //绑定键
    public final static String man = "topic.man";
    public final static String woman = "topic.woman";


    @Bean
    public Queue firstFan() {
        return new Queue("firstFan");
    }

    @Bean
    public Queue secondFan() {
        return new Queue("secondFan");
    }

    @Bean
    public Queue thirdFan() {
        return new Queue("thirdFan");
    }


    @Bean
    FanoutExchange fanoutExchange_1() {
        return new FanoutExchange("fanoutExchange_1");
    }

    @Bean
    FanoutExchange fanoutExchange_2() {
        return new FanoutExchange("fanoutExchange_2");
    }


    @Bean
    Binding bindingFirstFanout() {
        return BindingBuilder.bind(firstFan()).to(fanoutExchange_1());
    }

    @Bean
    Binding bindingSecondFanout() {
        return BindingBuilder.bind(secondFan()).to(fanoutExchange_1());
    }

    @Bean
    Binding bindingThirdFanout() {
        return BindingBuilder.bind(thirdFan()).to(fanoutExchange_2());
    }



    @Bean
    public Queue firstQueue() {
        return new Queue(man);
    }

    @Bean
    public Queue secondQueue() {
        return new Queue(woman);
    }

    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }


    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(topicExchange()).with(man);
    }

    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(topicExchange()).with("topic.#");
    }



    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("direct",false,false);//交换器名称、是否持久化、是否自动删除
    }

    @Bean
    public Queue queue() {
        return new Queue("direct");
    }
    @Bean
    Binding binding(Queue queue, DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("direct");
    }
}
